package mylab.commons.web.mvc.impl;

import io.swagger.annotations.ApiOperation;
import mylab.base.model.dto.Result;
import mylab.base.model.entity.Entity;
import mylab.commons.web.mvc.SuperCacheService;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;

/**
 * SuperCacheController
 * <p>
 * 继承该类，在SuperController类的基础上扩展了以下方法：
 * 1，get ： 根据ID查询缓存，若缓存不存在，则查询DB
 */
public abstract class SuperCacheControllerImpl<
        K extends Serializable,
        E extends Entity<K>,
        S extends SuperCacheService<K, E>,
        D>
        extends SuperControllerImpl<K, E, S, D> {

    @Override
    public Result<E> getById(K id) {
        return Result.newSuccess(service.getFromCache(id));
    }

    /**
     * 刷新缓存
     *
     * @return 是否成功
     */
    @ApiOperation(value = "刷新缓存", notes = "刷新缓存")
    @PostMapping("/cache/refresh")
    public Result<Boolean> refreshCache() {
        service.refreshCache();
        return Result.newSuccess(true);
    }

    /**
     * 清理缓存
     *
     * @return 是否成功
     */
    @ApiOperation(value = "清理缓存", notes = "清理缓存")
    @PostMapping("/cache/clear")
    public Result<Boolean> clearCache() {
        service.clearCache();
        return Result.newSuccess(true);
    }
}
